package dyyx.test1;

import java.util.concurrent.Executors;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class DisruptorTest {

	public static void main(String[] args) throws Exception {
		int ringBufferSize = 65536;
		final Disruptor<Data> disruptor = new Disruptor<Data>(new EventFactory<Data>() {
			public Data newInstance() {
				return new Data();
			}
		}, ringBufferSize, Executors.newSingleThreadExecutor(), ProducerType.SINGLE,
				// new BlockingWaitStrategy()
				new YieldingWaitStrategy());

		DataConsumer consumer = new DataConsumer();
		//
		disruptor.handleEventsWith(consumer);
		disruptor.start();

		new Thread(new Runnable() {
			public void run() {
				RingBuffer<Data> ringBuffer = disruptor.getRingBuffer();
				for (long i = 0; i < Consts.COUNT; i++) {
					long seq = ringBuffer.next();
					Data data = ringBuffer.get(seq);
					data.setId(i);
					data.setName("test" + i);
					ringBuffer.publish(seq);
				}
			}
		}).start();
		
		 // sleep 一下 ，查看GC数据 
        Thread.sleep(Consts.SLEEP_TIME);

	}
}
